package com.tuozixuan.thymeleaf.demo;


import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;

import com.tuozixuan.thymeleaf.demo.model.Person;

public class HtmlXmlThymeleafDemo {

	public static void main(String[] args) {
		
		ClassLoaderTemplateResolver htmlTemplateResolver = new ClassLoaderTemplateResolver();
		htmlTemplateResolver.setTemplateMode(TemplateMode.HTML);
		htmlTemplateResolver.setPrefix("/template/");
		htmlTemplateResolver.setSuffix(".html");
		
		ClassLoaderTemplateResolver xmlTemplateResolver = new ClassLoaderTemplateResolver();
		xmlTemplateResolver.setTemplateMode(TemplateMode.XML);
		xmlTemplateResolver.setPrefix("/template/");
		xmlTemplateResolver.setSuffix(".xml");
		
		Set<ITemplateResolver> templateResolvers = new HashSet<>();
		templateResolvers.add(htmlTemplateResolver);
		templateResolvers.add(xmlTemplateResolver);
		TemplateEngine templateEngine = new TemplateEngine();
		templateEngine.setTemplateResolvers(templateResolvers);
		
		Context context = new Context();
		Map<String, Object> map = new HashMap<>();
		map.put("name", "lisi");
		map.put("items", Arrays.asList("one", "two", "three"));
		Person person = new Person();
		person.setName("name1");
		Person person2 = new Person();
		person2.setName("name2");
		map.put("persons", Arrays.asList(person, person2));
		context.setVariables(map);
		
		String result = templateEngine.process("test.html", context);
		System.out.println(result);
		
		String result1 = templateEngine.process("test.xml", context);
		System.out.println(result1);
		
	}

}
